<?php
if (!defined('DC_CONTEXT_ADMIN')) { exit; }

$group 			= new Group($core->blog);
$customer		= new Customer($core->blog);
$permission		= new Permission($core->blog);
$debug 			= array();

// on déduit quel onglet doit etre actif
if (isset($_POST['part'])){
	$part = !empty($_POST['part']) && $_POST['part'] == 'permissions' ? 'permissions' : 'groupes';
} else {
	$part = !empty($_GET['part']) && $_GET['part'] == 'permissions' ? 'permissions' : 'groupes';
}


// on traite les formulaires

// formulaire d'ajout de groupe
if (!empty($_POST['groupname'])){ 
	$group->create($_POST['groupname']);
}
// formulaire de suppression de groupe
if(!empty($_GET['del'])){
	$group->delete($_GET['del']);
}

//formulaire d'allocation des utilisateurs a un groupe
if (!empty($_POST['customergroup'])){
	foreach($_POST['customergroup'] as $group_id=>$input){
		foreach($input as $value){
			$customer->init($value['customerid']);
			$customer->set_fields(array('group_id'=>$group_id));
			$customer->update();
		}
	}
}

//formulaire d'allocation des permissions aux groupes
if (!empty($_POST['grouppermission'])){
	$groups = $group->get_listGroup();
	foreach ($groups as $key=>$value){
		$rs = $core->blog->getCategories();
		while( $rs->fetch() ){ 
			if (isset($_POST['grouppermission'][$key][$rs->cat_id])){
				$permission->upd_permission($key,$rs->cat_id,1);
			}else{
				$permission->upd_permission($key,$rs->cat_id,0);
			}
		}
	}
}



?>
<html>
	<head>
  		<title><?php echo __('customersAuth'); ?></title>
  		<?php echo dcPage::jsPageTabs($part); ?>
  		<script type="text/javascript" src="index.php?pf=customersAuth/ui.core.js"></script>
		<script type="text/javascript" src="index.php?pf=customersAuth/ui.draggable.js"></script>
		<script type="text/javascript" src="index.php?pf=customersAuth/ui.droppable.js"></script>
  		<script type="text/javascript" src="index.php?pf=customersAuth/customersAuth.js"></script>
  		<style type="text/css">
  			@import 'index.php?pf=customersAuth/customersAuth.css';	
  		</style>
	</head>
	<body>
		
		<h2><?php echo html::escapeHTML($core->blog->name); ?> &gt; customersAuth</h2>
					
		<div id="groupes" class="multi-part" title="Gestion des Groupes">
			<form action="plugin.php?p=customersAuth" method="post">
				<fieldset style="clear:left;">
					<legend>Ajouter un groupe</legend>
					<input type="text" name="groupname"/><input type="submit" value="ok"/>
					<?php echo $core->formNonce(); ?>
				</fieldset>
			</form>
			
			<?php
			echo '<form method="post" action="plugin.php?p=customersAuth">'.
					'<input type="submit" value="mettre à jour" style="clear:both;width:auto;margin-bottom:5px;margin-left:5px;"/>'; 
			// on liste les groupes d'utilisateurs définie
			$groups = $group->get_listGroup();
			foreach ( $groups as $row1 ) {
				echo '<div>'.
					 '<fieldset class="groups" id="'.$row1['group_id'].'">'.
					 '<legend>'.$row1['group_name'].'</legend>';
				if($row1['group_id']!=0){
					 echo '<a class="trash" href="plugin.php?p=customersAuth&del='.$row1['group_id'].'">'.
					 		'<img src="index.php?pf=customersAuth/icons/trash.gif" /></a>';
				}
				echo '<div class="subgroups" >';
				// puis on liste les personnes alloué a chaque groupe
				$rs = $core->con->select('select * from '.$core->blog->prefix.'customer WHERE group_id='.$row1['group_id']);
				while ($rs->fetch()){
					echo 	'<div class="moveit">'.
							'<input type="hidden" name="customergroup['.$r->group_id.'][][customerid]" value="'.$rs->customer_id.'"/>'.
							'<h4>'.$rs->customer_login.'</h4>'.
							'</div>';
				}
				echo '</div></fieldset></div>';
			}
			echo $core->formNonce();
			echo '</form>';
			?>
		</div>
		
		
		
		<div id="permissions" class="multi-part" title="Gestion des permissions">
		<form method="post" action="plugin.php?p=customersAuth">
		<input type="hidden" name="part" value="permissions"/>
		<?php echo $core->formNonce();?>
		<fieldset><input type="submit" value="Mise à jour"/></fieldset>
		<?php
		$groups = $group->get_listGroup();
		foreach ( $groups as $row1 ) {
			echo '<table class="permissions">'.
				 '<tr><th>'.$row1['group_name'].'</th>'.
				 '<th><img src="index.php?pf=customersAuth/icons/eye.gif" alt="visible"/></th>'.
				 '</tr>';
			$rs = $core->blog->getCategories();
			while( $rs->fetch() ){
				echo '<tr><td>'
						.$rs->cat_title.
					 '</td><td>'.
					 '<input type="checkbox" name="grouppermission['.$row1['group_id'].']['.$rs->cat_id.']" '.
						($permission->get_Permission($row1['group_id'],$rs->cat_id)==1?'checked="checked"':'').
					'"/>'.
					 '</td></tr>';
			}
			echo '</table>';
		}
		?>
		</form>
		</div>
		
	</body>
</html>